package com.wintop.youyuan.dao.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.wintop.youyuan.dao.UserInformationDao;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.Photo;
import com.wintop.youyuan.util.CalculateUtil;

@Repository("userInformationDao")
public class UserInformationDaoImpl implements UserInformationDao {
	private SessionFactory sessionFactory;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public BaseData getBaseData(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from BaseData where userid=:id");
		query.setString("id", userid);
		return (BaseData) query.uniqueResult();
	}

	@Override
	public int getBaseDataId(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select dd.id from BaseData dd where userid=:id");
		query.setString("id", userid);
		return (Integer) query.uniqueResult();
	}

	@Override
	public String getIcon(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select dd.icon from BaseData dd where userid=:id");
		query.setString("id", userid);
		return (String) query.uniqueResult();
	}

	@Override
	public boolean updateData(Object data) {
		Session session = sessionFactory.getCurrentSession();
		session.update(data);
		session.flush();
		return true;
	}

	@Override
	public DetailData getDetailData(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from DetailData where userid=:id");
		query.setString("id", userid);
		return (DetailData) query.uniqueResult();
	}

	@Override
	public int getDetailDataId(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select dd.id from DetailData dd where userid=:id");
		query.setString("id", userid);
		return (Integer) query.uniqueResult();
	}

	@Override
	public PersonalsCondition getPersonalsCondition(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from PersonalsCondition where userid=:id");
		query.setString("id", userid);
		return (PersonalsCondition) query.uniqueResult();
	}

	@Override
	public int getPersonalsConditionId(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select dd.id from PersonalsCondition dd where userid=:id");
		query.setString("id", userid);
		return (Integer) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Photo> getPhotos(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Photo where userid=:id");
		query.setString("id", userid);
		return query.list();
	}

	@Override
	public boolean addPhoto(Photo photo) {
		photo.setTime(new Date(System.currentTimeMillis()));
		Session session = sessionFactory.getCurrentSession();
		session.save(photo);
		return true;
	}

	@Override
	public boolean deletePhoto(int id) {
		Session session = sessionFactory.getCurrentSession();
		Photo photo = new Photo();
		photo.setId(id);
		session.delete(photo);
		return true;
	}

	@Override
	public int getLoveNum(int sex, String province, String city, int minAge,
			int maxAge) {
		System.out.println("传进来了：" + province + "*" + city + "*" + sex);
		Session session = sessionFactory.getCurrentSession();
		String sql = getLoveSql(sex, province, city, minAge, maxAge, -1, 0);
		Query query = session.createSQLQuery(sql);
		@SuppressWarnings("unchecked")
		List<BigInteger> list = query.list();
		int count = list.get(0).intValue();
		System.out.println("查询到了：" + count);
		return count;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseData> getLoveBaseDatas(int sex, String province,
			String city, int minAge, int maxAge, int pageNum, int pageSize) {
		Session session = sessionFactory.getCurrentSession();
		String sql = getLoveSql(sex, province, city, minAge, maxAge, pageNum,
				pageSize);
		Query query = session.createSQLQuery(sql).addEntity(BaseData.class);
		return query.list();
	}

	/*
	 * 获取搜索sql语句
	 */
	private String getLoveSql(int sex, String province, String city,
			int minAge, int maxAge, int pageNum, int pageSize) {

		String sql;
		if (pageNum != -1) {
			sql = "select bd.*,user.* ";
		} else {
			sql = "select count(user.id) ";
		}
		sql += "from baseData as bd left join user on bd.userid=user.userid where user.sex="
				+ sex + " and bd.apartment like ";
		if (city == null) {
			sql += "'%" + province + "%'";

		} else {
			sql += "'%" + province + "-" + city + "%'";
		}
		if (minAge == 0) {
			if (maxAge != 0) {
				String maxAgeStr = CalculateUtil.getBirthday(maxAge);
				sql += " and bd.birthday >= '" + maxAgeStr + "'";
			}
		} else {
			if (maxAge == 0) {
				String minAgeStr = CalculateUtil.getBirthday(minAge);
				sql += " and bd.birthday <= '" + minAgeStr + "'";
			} else {
				String minAgeStr = CalculateUtil.getBirthday(maxAge);
				String maxAgeStr = CalculateUtil.getBirthday(minAge);
				sql += " and bd.birthday Between '" + minAgeStr + "' and '"
						+ maxAgeStr + "'";
			}
		}
		if (pageNum != -1) {
			sql += " limit " + pageNum + "," + pageSize;
		}
		return sql;
	}

}
